<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Activity;


use App\Http\Controllers\Controller;
use App\Models\ActivityLuck;
use App\Models\ActivityLuckDetail;
use App\Models\ActivityLuckDetailLog;
use App\Models\Bomb;
use App\Models\Good;
use App\Models\GoodsStock;
use App\Models\Order;
use App\Models\Red;
use App\Models\RedRole;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserIntegralRedLog;
use App\Models\VipLog;
use App\Services\Manager\GoodsService;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class BombController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Activity\BombController";

    public function index(Request $request)
    {
        $bomb = Bomb::first();
        $goodsName = $request->goodsName;
        $pageSize = $request->pageSize;

        $sql = Good::select('id','name','bombImage','bombPrice','bombIndex')
            ->where('status',30);
        if (isset($goodsName)) {
            $sql->where('name', 'like',"%$goodsName%");
        }
        $goodList = $sql->orderByDesc('bombIndex')
            ->orderByDesc('bombIndexTime')
            ->paginate($pageSize)
            ->toArray();
        $bombList = $goodList['data'];
        foreach ($bombList as $key => $value) {
            $bombList[$key]['bombImage'] = path($value['bombImage']);
        }
        $bomb['bombList'] = $bombList;
        $bomb['totalNum'] = $goodList['total'];
        return $this->apiResponse($bomb);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $data = $request->only('detail');
        if (!$data['detail']) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        Bomb::where('bombId','!=',0)->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = "修改";
        $content = "修改了每日爆款的规则为【" . $data['detail'] . "】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "每日爆款模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $bomb = Bomb::first();
        if($bomb['status']){
            $content = "禁用";
            Bomb::where('bombId','!=',0)->update(['status'=>0]);
        }else{
            $num = Good::where('status', 30)->count();
            if(!$num){
                return $this->apiResponse('', config('errorLiu.KAIQIHUODONGZHISHAOXUYAOYIJ'));
            }
            $content = "启用";
            Bomb::where('bombId','!=',0)->update(['status'=>1]);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content .= "了每日爆款";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "每日爆款模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }


    public function bombDetail(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only("bombImage", 'bombPrice');
        if (!isset($id) || !$id || !isset($data['bombImage'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $data['status'] = 30;
        $good = Good::where('id', $id)->first();
        $goodService = new GoodsService();
        $res = $goodService->verify($good,$id);
        if($res == -1){
            return $this->apiResponse('', config('errorLiu.GONGYINGSHANGYITINGZHIHEZ'));
        }
        if ($res == -2) {
            return $this->apiResponse('', config('errorLiu.GASHANGPINXUANCZEZHONGLIANL'));
        }
        if ($res == -3) {
            return $this->apiResponse('', config('errorLiu.GAISHANGPINZANWUGUIGE'));
        }
        if ($res == -4) {
            return $this->apiResponse('', config('errorLiu.GAISHANGPGUIGESEWEIWZ'));
        }
        if ($res == -5) {
            return $this->apiResponse('', config('errorLiu.SHANGPINGUIGEZANWUTUPIANQING'));
        }
        if ($res == -6) {
            return $this->apiResponse('', config('errorLiu.SHANGPINGWEITIANXIEHANSUIHUIBUHAN'));
        }
        Good::where('id',$id)->where('is_del',0)->whereIn('status',[2,30])->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/bombDetail";
        $title = "新增";
        $content = "添加修改了每日爆款的商品,商品id为【".$id."】,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "每日爆款模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function bombIndex(Request $request,$id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Good::where('id',$id)->update([
            'bombIndex'=>1,
            'bombIndexTime'=>date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/bombIndex";
        $title = "修改";
        $content = "置顶了id为【".$id."】的商品";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "每日爆款模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request,$id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Good::where('id',$id)->update([
            'bombIndex'=>0,
            'status'=>2
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的商品";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "每日爆款模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}